<template>
<!-- 筛选区 -->
 <div class="sub-filter">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a  href="javasript:;" v-for="i in subObj.brands" :key="i.id">
            {{i.name}}
        </a>
      </div>
    </div>
    <!-- 规格 -->
    <div class="item" v-for="i in subObj.saleProperties" :key="i">
      <div class="head">{{i.name}}：</div>
      <div class="body">
        <a href="javasript:;" v-for="item in i.properties" :key="item.id" @click="changeAttr(item,i)">{{item.name}}</a>
      </div>
    </div>
  </div>
</template>
<script>
import { findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'
import { onMounted, ref, inject } from 'vue'
export default {
  name: 'SubBread',
  setup () {
    const route = useRoute()
    const subObj = ref({})
    async function getsubObj () {
    //   const subId = route.params.id
      const res = await findSubCategoryFilter(route.params.id)
      console.log('二级分类数据', res)
      subObj.value = res.data.result
      //   在品牌和规则的头部添加一个全部类别
      const result = res.data.result
      result.brands.unshift({
        id: null,
        name: '全部'
      })
      //   类别加全部
      result.saleProperties.forEach(item => {
        item.properties.unshift({
          id: null,
          name: '全部'
        })
      })
      subObj.value = result
      console.log('filter数据', result)
    }
    onMounted(() => {
      getsubObj()
    })
    // 引入setBandID方法
    const setBrandId = inject('setBrandId')
    const setAttrs = inject('setAttrs')
    function changeAttr (item, curGroup) {
      console.log('看看item什么样', item, curGroup)
      setAttrs({
        groupName: curGroup.name,
        propertyName: item.name
      })
    }
    return { subObj, setBrandId, setAttrs, changeAttr }
  }
}
</script>
<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      padding: 10px 0;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
</style>
